﻿/*
Copyright (c) 2010 Lance Ward
This software is licensed under the MIT license, except where otherwise noted.
For complete license details, refer to license.txt
*/

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ADNLib;

namespace ADN
{

    public partial class FormMotions : Form
    {
		private enum EditMode
		{
			Edit,
			Create,
			None
		}

		private List<Motion> motions;
		private EditMode mode = EditMode.None;

        public FormMotions()
        {
            InitializeComponent();
			Initialize();
        }

		private void Initialize()
		{
			if (motions != null)
			{
				lstMotions.DataSource = motions;
				lstMotions.DisplayMember = "MotionName";
				EnableInput(false);
			}
			else { DisableAll(); }
		}

		public List<Motion> Motions
		{
			get { return motions; }
			set
			{
				motions = value;
				Initialize();
			}
		}

		private void lstMotions_SelectedIndexChanged(object sender, EventArgs e)
		{
			int index = lstMotions.SelectedIndex;
			if (index >= 0)
			{
				txtMotionName.Text = motions[index].MotionName;
				cmboMotionType.Text = motions[index].MotionType.ToString();
				spinStartFrame.Value = motions[index].StartFrame;
				spinDuration.Value = motions[index].Duration;
			}
			else { ClearInput(); }
		}

		private void btnAdd_Click(object sender, EventArgs e)
		{
			EnableInput(true);
		}

		private void btnRemove_Click(object sender, EventArgs e)
		{

		}

		private void cmboMotionType_SelectionChangeCommitted(object sender, EventArgs e)
		{

		}

		private void txtMotionName_TextChanged(object sender, EventArgs e)
		{

		}

		private void spinStartFrame_ValueChanged(object sender, EventArgs e)
		{

		}

		private void spinDuration_ValueChanged(object sender, EventArgs e)
		{

		}

		private void btnSettings_Click(object sender, EventArgs e)
		{

		}

		private void btnEdit_Click(object sender, EventArgs e)
		{
			EnableInput(true);
		}

		private void btnSave_Click(object sender, EventArgs e)
		{
			int index = lstMotions.SelectedIndex;
			motions[index].MotionName = txtMotionName.Text;
			if (!String.IsNullOrEmpty(cmboMotionType.Text))
			{
				motions[index].MotionType = (MotionType)Enum.Parse(typeof(MotionType), cmboMotionType.Text, true);
			}
			else{ motions[index].MotionType = MotionType.None;}
			
			motions[index].StartFrame = (int)spinStartFrame.Value;
			motions[index].Duration = (int)spinDuration.Value;

			EnableInput(false);
		}

		private void btnCancel_Click(object sender, EventArgs e)
		{
			ClearInput();
			lstMotions.SelectedIndex = 0;
			EnableInput(false);
		}

		private void EnableInput(bool enable)
		{
			txtMotionName.Enabled = enable;
			cmboMotionType.Enabled = enable;
			spinStartFrame.Enabled = enable;
			spinDuration.Enabled = enable;
			btnSettings.Enabled = enable;
			btnSave.Enabled = enable;
			btnCancel.Enabled = enable;
			btnEdit.Enabled = !enable;
			btnAdd.Enabled = !enable;
			btnRemove.Enabled = !enable;
			lstMotions.Enabled = !enable;
		}

		private void DisableAll()
		{
			txtMotionName.Enabled = false;
			cmboMotionType.Enabled = false;
			spinStartFrame.Enabled = false;
			spinDuration.Enabled = false;
			btnSettings.Enabled = false;
			btnSave.Enabled = false;
			btnCancel.Enabled = false;
			btnEdit.Enabled = false;
			btnAdd.Enabled = false;
			btnRemove.Enabled = false;
			lstMotions.Enabled = false;
		}

		private void ClearInput()
		{
			txtMotionName.Clear();
			cmboMotionType.Text = String.Empty;
			spinStartFrame.Value = 0;
			spinDuration.Value = 0;
		}
	}
}
